/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;

import com.cea.papyrus.core.PapyrusPlugin;


/**
 * 
 */
public class ContainerHighlightEditPolicy 
extends org.eclipse.gef.editpolicies.GraphicalEditPolicy
{

	/**
	 * 
	 */
	private Color revertColor;

	/**
	 * 
	 * 
	 * @param request 
	 */
	@Override
	public void eraseTargetFeedback(Request request){
		if (revertColor != null){
			setContainerBackground(revertColor);
			revertColor = null;
		}
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	private Color getContainerBackground(){
		return getContainerFigure().getBackgroundColor();
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	private IFigure getContainerFigure(){
		return ((GraphicalEditPart)getHost()).getFigure();
	}

	/**
	 * 
	 * 
	 * @param request 
	 * 
	 * @return 
	 */
	@Override
	public EditPart getTargetEditPart(Request request){
		return request.getType().equals(RequestConstants.REQ_SELECTION_HOVER) ?
				getHost() : null;
	}

	/**
	 * 
	 * 
	 * @param c 
	 */
	private void setContainerBackground(Color c){
		getContainerFigure().setBackgroundColor(c);
	}

	/**
	 * 
	 */
	protected void showHighlight(){
		if (revertColor == null){
			revertColor = getContainerBackground();
			setContainerBackground(PapyrusPlugin.colorManager.get(new RGB( 200, 200, 240)));
		}
	}

	/**
	 * 
	 * 
	 * @param request 
	 */
	@Override
	public void showTargetFeedback(Request request){
		if(request.getType().equals(RequestConstants.REQ_MOVE) ||
				request.getType().equals(RequestConstants.REQ_ADD) ||
				request.getType().equals(RequestConstants.REQ_CLONE) ||
				request.getType().equals(RequestConstants.REQ_CONNECTION_START) ||
				request.getType().equals(RequestConstants.REQ_CONNECTION_END) ||
				request.getType().equals(RequestConstants.REQ_CREATE)
		) {
			showHighlight();
		}
	}

}
